package net.oschina.git.fsppp.view.frame;

import net.oschina.git.fsppp.common.base.BaseEvent;
import net.oschina.git.fsppp.common.base.BaseFrame;
import net.oschina.git.fsppp.model.P3Dir;
import net.oschina.git.fsppp.view.contract.PropertyContract;
import net.oschina.git.fsppp.view.model.PropertyModel;
import net.oschina.git.fsppp.view.presneter.PropertyPresenter;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * Created by QiHuang on 2016/10/21.
 */
public class PropertyView extends BaseFrame<PropertyContract.Presenter, PropertyContract.Model>
        implements PropertyContract.View {
    private P3Dir dir;
    private JLabel path, path1, attr, attr1, start, start1, length, length1, size, size1;
    private JPanel panel1, panel2, panel3, panel4, panel5, panel6;
    private JButton btnOK;
    /**
     * 文件夹总容量
     */
    private int total = 0;

    @Override
    public void returnChildDirs(List<P3Dir> dirs) {
        for (P3Dir dir : dirs) {
            if (dir.getType() == P3Dir.TYPE_OF_DIR) {
                mPresenter.getChildDirs(dir);
            } else {
                total += ((dir.getContent() == null) ? 0 : dir.getContent().length);
            }
        }
        size1.setText(total + " bit");
    }

    @Override
    protected void initView() {
        dir = (P3Dir) BaseEvent.getDefault().getParam();
        path = new JLabel("路径:");
        path1 = new JLabel();
        attr = new JLabel("文件后缀:");
        attr1 = new JLabel();
        start = new JLabel("起始块号:");
        start1 = new JLabel();
        length = new JLabel("文件长度:");
        length1 = new JLabel();
        size = new JLabel("大小:");
        size1 = new JLabel();
        btnOK = new JButton("返回");

        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4 = new JPanel();
        panel5 = new JPanel();
        panel6 = new JPanel();

        panel1.add(path);
        panel1.add(path1);
        panel2.add(attr);
        panel2.add(attr1);
        panel3.add(start);
        panel3.add(start1);
        panel4.add(length);
        panel4.add(length1);
        panel5.add(size);
        panel5.add(size1);
        panel6.add(btnOK);

        panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
        panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
        panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
        panel4.setLayout(new FlowLayout(FlowLayout.CENTER));
        panel5.setLayout(new FlowLayout(FlowLayout.CENTER));
        panel6.setLayout(new FlowLayout(FlowLayout.CENTER));

        add(panel1);
        add(panel2);
        add(panel3);
        add(panel4);
        add(panel5);
        add(panel6);

        setLayout(new GridLayout(6, 1 ,5, 10));
        setSize(300, 400);
        path1.setText(dir.getPath());
        start1.setText(dir.getStartNum() + "");
        length1.setText(dir.getLength() + "");
        if (dir.getType() == P3Dir.TYPE_OF_FILE) {
            attr1.setText("." + dir.getAttr());
            size1.setText((dir.getContent() == null? String.valueOf(0) : dir.getContent().length) + " bit");
        } else {
            attr1.setText("无后缀");
            mPresenter.getChildDirs(dir);
        }

    }

    @Override
    protected void initPM() {
        mModel = new PropertyModel();
        mPresenter = new PropertyPresenter(this, mModel);
    }

    @Override
    protected void initListener() {
        btnOK.addActionListener(e -> {
            dispose();
        });
    }

    @Override
    protected String getFrameTitle() {
        return (dir.getType() == P3Dir.TYPE_OF_DIR? "文件夹:" : "文件:")
                + dir.getName();
    }
}
